package com.xhwl.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.enums.*;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.pojo.cent.sdata.devplatform.Developer;
import com.xhwl.common.query.cent.sdata.AppQuery;
import com.xhwl.data.service.IAppService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/8/16 15:32
 */
@RestController
public class AppController {
    private static final Logger log = LoggerFactory.getLogger(AppController.class);
    @Autowired
    IAppService appService;

    /**
     * app选择器
     *
     * @return
     */
    @GetMapping(value = "/app/list")
    public ResultJson getAppList(@RequestHeader(value = "appId", required = true) String appId,
                                 @RequestHeader(value = "clientType", required = false) Integer clientType,
                                 @RequestHeader(value = "industryId", required = true) Short industryId,
                                 AppQuery appQuery) {
        List<Integer> appTypeCodes = new ArrayList<>();
        if (null != appQuery.getSourceClient()) {
            if (SourceClientEnum.MOBILE.getType() == appQuery.getSourceClient()) {
                appTypeCodes.add(AppTypeEnum.APP.getType());
                appTypeCodes.add(AppTypeEnum.MINI_PROGRAM.getType());
            }else if (SourceClientEnum.PLATFORM.getType() == appQuery.getSourceClient()) {
                appTypeCodes.add(AppTypeEnum.WEB.getType());
            }else{
                return ResultJson.fail("暂不支持 source client 类型");
            }
        }
        //设置app类型
        if (!CollectionUtils.isEmpty(appTypeCodes)) {
            appQuery.setAppTypeCodes(appTypeCodes);
        }
        appQuery.setIndustryId(industryId);
        List<App> list = appService.list(appQuery);
        return ResultJson.success(list);
    }

    /**
     * 分页查询
     * @param appQuery
     * @return
     */
    @PostMapping("/app/page/v2")
    public ResultJson page(@RequestBody AppQuery appQuery, Developer developer){
        if(null == appQuery.getDevelopId()){
            appQuery.setDevelopId(developer.getId());
        }
        Page<App> resultPage = appService.findByPage(appQuery, appQuery.getPage());
        return ResultJson.success(resultPage);
    }

    /**
     * 获取app名称
     *
     * @param appId
     * @param clientType
     * @param industryId
     * @return
     */
    @GetMapping(value = "/app/get-name")
    public ResultJson getName(@RequestHeader(value = "appId", required = true) String appId,
                              @RequestHeader(value = "clientType", required = false) Integer clientType,
                              @RequestHeader(value = "industryId", required = true) Short industryId) {
        App app = appService.getByAppId(appId);
        if (Objects.isNull(app)) {
            log.info("当前数据库无此appId={}相关数据，请核实",appId);
            return ResultJson.fail("app数据异常");
        }
        String appName = app.getName();
        return ResultJson.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getDescription(), appName);
    }

    /**
     * 应用创建&更新
     * @param app
     * @return
     */
    @PostMapping("/app/save-or-update")
    public ResultJson update(@RequestBody App app) {
        if (null == app.getId()) {
            appService.create(app);
        } else {
            appService.update(app);
        }
        return ResultJson.success();
    }

    /**
     * 应用类型
     * @return
     */
    @PostMapping("/app/app-type")
    public ResultJson getAppType() {
        return appService.getAppType();
    }

    /**
     * 根据应用类型获业务类型
     * @param appType
     * @return
     */
    @GetMapping("/app/business-type")
    public ResultJson businessType(@RequestParam("appType") Short appType) {
        List<AppBusinessTypeEnum> appBusinessTypeEnums = AppBusinessTypeEnum.valuesOf(AppTypeEnum.valueOfId(appType));
        List<JSONObject> list = new ArrayList<>(appBusinessTypeEnums.size());
        if (!CollectionUtils.isEmpty(appBusinessTypeEnums)) {
            for (AppBusinessTypeEnum appBusinessTypeEnum : appBusinessTypeEnums) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", appBusinessTypeEnum.id);
                jsonObject.put("key", appBusinessTypeEnum.name());
                jsonObject.put("name", appBusinessTypeEnum.description);
                list.add(jsonObject);
            }
        }
        return ResultJson.success(list);
    }

    /**
     * 时效状态列表（应用状态）
     * @return
     */
    @GetMapping("/app/valid-state")
    public ResultJson validState() {
        ValidStateTypeEnum[] values = ValidStateTypeEnum.values();
        ArrayList<JSONObject> list = new ArrayList<>(values.length);
        for (ValidStateTypeEnum value : values) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", value.id);
            jsonObject.put("key", value.name());
            jsonObject.put("name", value.description);
            list.add(jsonObject);
        }
        return ResultJson.success(list);
    }

}
